<?php
/**
 * name 刘杨
 * 用户
 */
namespace backend\controllers;
use Yii;
use backend\models\User;
use yii\data\Pagination;
use backend\models\AdminRole;
class UserController extends \yii\web\Controller
{ 
	public $layout = 'admin_main';
    function init()
    {
        $session = Yii::$app->session;
        if(empty($session->get('admin_user')))
        {
            $this->redirect('?w=login/login');
        }
    }
    //展示用户
    public function actionIndex()
    {
        $sou = Yii::$app->request->get('sou')?Yii::$app->request->get('sou'):'';
        $page = Yii::$app->request->get('page')?Yii::$app->request->get('page'):'1';
        $model = new User;
        $model->status = 1;
        if($sou!='')
        {
            $User = $model->sousuo($sou);
        }
        else
        {
            $user = User::find();
            $pagination = new Pagination([
                'defaultPageSize' => 7,
                'totalCount' => $user->where(['status'=>1])->count(),
            ]);
            $data = $user->offset($pagination->offset)
            ->limit($pagination->limit)
            ->orderBy('id desc')
            ->where(['status'=>1])
            ->asArray()
            ->all();
            $User['data'] = $data;
            $User['pagination'] = $pagination;
        }
        $User['page'] = $page;
        $User['model'] = $model;
        if(!empty($User['data']))
        {
            // 分页后用户ID
            $user_id = [];
            foreach ($User['data'] as $k => $v) {
                $user_id[$k] = $v['id'];
            }
            //用户角色
            $role = new AdminRole;
            $User['role_name'] = $role->userRole($user_id);
        }
        //查询所有角色
        $User['role'] = AdminRole::find()->asArray()->all();
        return $this->render('user',$User);
    }
    //添加用户
    function actionAdd()
    {
        $user = new User;
        $data = Yii::$app->request->post('User');
        $user->name = $data['name'];
        $user->pwd = $data['pwd'];
        $user->realname = $data['realname'];
        $user->phone = $data['phone'];
        $user->status = $data['status'];
        $user->addtime = time();
        
        if($user->insert())
        {
            $user_id = $user->attributes['id'];
            if(!empty($data['role_id']))
            {
                $ur_id = [];
                foreach ($data['role_id'] as $k => $v) {
                    array_push($ur_id,[$user_id,$v]);
                }
                Yii::$app->db->createCommand()->batchInsert('admin_user_role',[
                    'user_id','role_id'],$ur_id)->execute();
            }
            $this->redirect(['index']);
        }
    }
    //修改用户信息
    function actionUpdate()
    {
        if($data = Yii::$app->request->post())
        {
            // var_dump('<pre>',$data);exit;
            $save = User::findOne($data['id']);
            $save->name = $data['name'];
            $save->pwd = $data['pwd'];
            $save->realname = $data['realname'];
            $save->phone = $data['phone'];
            $save->status = $data['status'];
            if($save->save())
            {
                Yii::$app->db->createCommand()->delete('admin_user_role','user_id = '.$data['id'])->execute();
                $ur_id = [];
                foreach ($data['role_id'] as $k => $v) {
                    array_push($ur_id,[$data['id'],$v]);
                }
                Yii::$app->db->createCommand()->batchInsert('admin_user_role',[
                    'user_id','role_id'],$ur_id)->execute();
                exit(json_encode(array('error'=>1,'msg'=>'修改成功')));
            }else
            {
                exit(json_encode(array('error'=>0,'msg'=>'修改失败')));
            }
        }
        $id = Yii::$app->request->get('id');
        $select = User::find()->where(['id'=>$id])->asArray()->one();
        $role = Yii::$app->db->createCommand('select role_id from admin_user_role where user_id = '.$id)->queryAll();
        $role_id = [];
        foreach ($role as $k => $v) {
            $role_id[] = $v['role_id'];
        }
        $select['role_id'] = $role_id;
        exit(json_encode(array('error'=>1,'msg'=>$select)));
    }
    //删除用户
    function actionDel(){
       $id = Yii::$app->request->get('id');
       $User = User::findOne($id);
       $User->status = 0;
       if($User->save())
       {
            exit(json_encode(array('error'=>1)));
       }else
       {
            exit(json_encode(array('error'=>0)));
       }
    }
}
